{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO 02-16 07:08:09 __init__.py:190] Automatically detected platform cuda.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/huanli/programs/miniconda3/envs/unsafe/lib/python3.10/site-packages/tree_sitter/__init__.py:36: FutureWarning: Language(path, name) is deprecated. Use Language(ptr, name) instead.\n",
      "  warn(\"{} is deprecated. Use {} instead.\".format(old, new), FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import os\n",
    "import utils.batch_utils as bu\n",
    "import utils.LLM_utils as llm\n",
    "from utils.file_utils import dir_check, copy_to\n",
    "from batch_eval_for_vllm import BatchAnalyzer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_dir = \"prompt/\"\n",
    "sample_dir = \"data/samples/\"\n",
    "result_root_dir = f\"result/\"\n",
    "GPT_4 = \"gpt-4o\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def upload_batch(analyzer: BatchAnalyzer, sample_target: str):\n",
    "    batch_file = analyzer.batch_file(sample_target)\n",
    "    metadata = analyzer.batch_util.upload_and_create_batch(batch_file)\n",
    "    batch_id = metadata.id\n",
    "    if os.path.exists(analyzer.batch_id_dict_file):\n",
    "        batch_id_dict = json.load(open(analyzer.batch_id_dict_file))\n",
    "    else:\n",
    "        batch_id_dict = dict()\n",
    "    batch_id_dict[sample_target] = batch_id\n",
    "    json.dump(batch_id_dict, open(analyzer.batch_id_dict_file, \"w\"), indent=2)\n",
    "    return metadata\n",
    "\n",
    "def download_batch_result(analyzer: BatchAnalyzer, sample_target: str):\n",
    "    batch_id_dict = json.load(open(analyzer.batch_id_dict_file))\n",
    "    batch_id = batch_id_dict[sample_target]\n",
    "    batch_result_file = analyzer.batch_result_file(sample_target)\n",
    "    analyzer.batch_util.retrieve_batch_result(batch_id, batch_result_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparative Experiments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_targets = [\"risky\", \"filtered_unsafe\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### LLM with Safe4U"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"Safe4U\"\n",
    "analyzers = []\n",
    "analyzers.append(BatchAnalyzer(prompt, GPT_4))\n",
    "analyzers.append(BatchAnalyzer(\"basic_check\", GPT_4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in sample_targets:\n",
    "    for analyzer in analyzers:\n",
    "        analyzer.generate_batch(t)\n",
    "        upload_batch(analyzer, t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Typically, openai takes about 12 hours to process the batch\n",
    "for t in sample_targets:\n",
    "    for analyzer in analyzers:\n",
    "        # download_batch_result(analyzer, t)\n",
    "        analyzer.resolve_batch_result(t)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "unsafe",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
